package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.SaleCarBean;
import com.project.mapper.ISaleCarMapper;
import com.project.service.ISaleCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SaleCarServiceImpl implements ISaleCarService {
    @Autowired
    private ISaleCarMapper carMapper;

    @Override
    public List<SaleCarBean> findByUserId(Integer userId) {
        return carMapper.findUserId(userId);
    }

    @Override
    public void add(Integer number, Integer userId) {
        SaleCarBean sellCar = carMapper.findById(userId,number);
        if (sellCar == null){
            sellCar = new SaleCarBean(number,userId);
            carMapper.insert(sellCar);
        }else {
            sellCar.setNum(sellCar.getNum() + 1);
            carMapper.updateById(sellCar);
        }
    }

    @Override
    public void del(Integer saleCarId) {
        carMapper.deleteById(saleCarId);
    }

    @Override
    public void update(Integer saleCarId, Integer num) {
        SaleCarBean carBean = new SaleCarBean();
        carBean.setId(saleCarId);
        carBean.setNum(num);
        carMapper.updateById(carBean);
    }

    @Override
    public Integer getTotalPrice(Integer userId) {
        return carMapper.getTotalPrice(userId);
    }
}
